
$(function(){

    var action_name = get_current_action_name();

    //授权登录页面
    if(action_name=="sign_in" || action_name=="refresh_location"){
        refresh_location();
    }


    //主页面
    if(action_name=="index"){
        var mySwiper = new Swiper ('#carrousel .swiper-container', {
            direction: 'horizontal',
            autoplay: 5000,
            loop: true,
            // 如果需要分页器
            pagination: '.swiper-pagination'
        });

        foot_bar_init();
        //本地存储
        if(typeof(sessionStorage)!=="undefined"){
            console.log("当前浏览器，支持 sessionStorage 对象!");
            var current_userinfo_id = $("#current_userinfo_id").val();
            var current_business_id = $("#current_business_id").val();
            var current_customer_id = $("#current_customer_id").val();
            var current_customer_mobile = $("#current_customer_mobile").val();
            sessionStorage.userinfo_id = current_userinfo_id;
            sessionStorage.business_id = current_business_id;
            sessionStorage.customer_id = current_customer_id;
            sessionStorage.customer_mobile = current_customer_mobile;

        }else{
            console.log("抱歉! 不支持 web 存储。!");
        }
    }else if(action_name=="product_search"){//商品搜索
        foot_bar_init();
    }else if(action_name=="product_show"){//商品展示

        var mySwiper = new Swiper ('#carrousel .swiper-container', {
            direction: 'horizontal',
            autoplay: 5000,
            loop: true,
            // 如果需要分页器
            pagination: '.swiper-pagination'
        });
    }

    shoppingcart_listener_event_init();
    //$('#category-container').on('touchend',function(e) {
    //    //var _x=e.touches[0].pageX;
    //    //var _y=e.touches[0].pageY;
    //    console.log("拖拉结束监听："+ e.type);
    //    //e.preventDefault();
    //});

    reset_foot_icon_by(action_name);

})


function search_change(value){
    console.log("搜索文本改变为："+value);
    if(value!=''){
        $("#search_show").show();
    }else{
        $("#search_show").hide();
        $("#search_bar").removeClass('weui_search_focusing');
    }
}

function search_cancelsss(){

    console.log("搜索取消按钮点击");
}


//首页
function index(){
    reset_foot_icon('index');
    go_index();
}
//分类
function category(){

    reset_foot_icon('category');

    if($("#category-container").css("display")=='none'){
        $("#category-container").show();
    }else{
        $("#category-container").hide();
    }


}

//购物车
function cart(){

    reset_foot_icon('cart');
    cart_click();
}

//订单
function order(){

    reset_foot_icon('order');
    go_order();
}

function reset_foot_icon(flag){

    console.log("点击按钮标识为："+flag);
    $(".weui_tabbar a[data-info='"+flag+"'] img").attr("src",'/root/icon_'+flag+'_active.png');

    $(".weui_tabbar a[data-info!='"+flag+"']").each(function(){
        $(this).find("img").attr("src",'/root/icon_'+$(this).attr('data-info')+'.png');
    })

    if(flag!='category'){
        $("#category-container").hide();
    }
}

//更具action名称设置底部图标
function reset_foot_icon_by(action_name){

    if(action_name=="index"){
        reset_foot_icon('index')
    }else if(action_name=="product_search" ){
        reset_foot_icon('category')
    }
}

//立刻购买
function instant_to_buy(){

    var product_list = "";
    $("#inner-op .product-cell").each(function(){

        var number = $(this).find("[name='product_number']").val();
        var product_id = $(this).find("[name='product_id']").val();

        if(number=="" || product_id=="" ) return;

        if(product_list==""){
            product_list = product_id+":"+number
        }else{
            product_list += ","+product_id+":"+number
        }
    });

    if(product_list==""){
        jyd_toast({'content':'亲，请添加商品！'});
        return;
    }

    location.href = "/order/new?product_list="+product_list+"&product_source=shoppingcart";
}

//立刻购买
function instant_to_buy_for_one(product_id){

    jyd_loading_toast({'content':'处理中……'});
    var product_list = product_id+":"+1;
    location.href = "/order/new?product_list="+product_list;
}

function search_product(category_id){

    jyd_loading_toast({'content':'处理中……'});

    var keyword = $("#search_input").val();

    if((typeof(keyword)!="string" || keyword=='') && category_id==""){
        jyd_toast({'content':'亲，请输入关键词！'})
        return;
    }

    if(typeof(category_id)!="string"){
        category_id = "";
    }

    console.log("搜索关键词为："+keyword);
    location.href = "/product_search?keyword="+$("#search_input").val()+"&category_id="+category_id;
}


//底部导航栏初始化
function foot_bar_init(){

    //首页分类遮罩层设置
    $("#category-container").click(function(){
        console.log("分类遮罩层点击！");
        $("#category-container").hide();
    });

    $("#category-container .category-menu").click(function(event){
        event.stopPropagation();
        console.log("分类菜单点击！");
    });
}

function event_stop(event){
    event.stopPropagation();
    console.log("时间阻止！");
}
